<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <!-- <img src="../assets/bar.jpg" height="60" alt=""> -->
        <div style="font-size: 26px; font-weight: bold; color: #ffffff;padding-left: 35px;">
          中移政企部OA
        </div>
        <span style="color:#409EFF ;"></span>
      </div>
      <div>
        <span style="color: #ffffff; padding-right: 10px; font-size: 14px;">欢迎，{{name}}</span>
        <el-dropdown class="avatar-container right-menu-item hover-effect">
          <div class="avatar-wrapper">
            <!-- <img src="../assets/user1.jpeg" class="user-avatar"> -->
            <img src="../assets/user2.jpeg" class="user-avatar"><i class="el-icon-caret-bottom" style="color: #ffffff;"/>
          </div>
          <el-dropdown-menu slot="dropdown">
            <!-- <router-link to="/profile/index">
              <el-dropdown-item>个人中心</el-dropdown-item>
            </router-link>
            <router-link to="/">
              <el-dropdown-item>控制台</el-dropdown-item>
            </router-link> -->
            <el-dropdown-item @click.native="editDialogVisible = true">
              <span style="display:block;">修改密码</span>
            </el-dropdown-item>
            <el-dropdown-item divided @click.native="logout">
              <span style="display:block;">退出登录</span>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
      <!-- <el-button type="info" @click="logout">退出</el-button> -->
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单区域 -->
        <!-- #333744 -->
        <el-menu background-color="#364e6e" text-color="#fff" active-text-color="#409EFF" unique-opened :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.iconid]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/' + subItem.path)">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
        <!-- 修改密码的对话框 -->
        <el-dialog title="修改密码" :visible.sync="editDialogVisible" width="35%" @close="editDialogClosed" :close-on-click-modal="false">
          <!-- 内容主体区域 -->
          <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">
            <el-form-item label="新密码" prop="mima">
              <el-input v-model="editForm.mima" style="width: 400px;"></el-input>
            </el-form-item>
          </el-form>
          <!-- 底部区域 -->
          <span slot="footer" class="dialog-footer">
            <el-button @click="editDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="editmima">确 定</el-button>
          </span>
        </el-dialog>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menulist: [],
      iconsObj: {
        '1': 'iconfont icon-shangpin',
        '2': 'iconfont icon-user',
        '3': 'iconfont icon-3702mima',
        '4': 'iconfont icon-users',
        '6': 'iconfont icon-tijikongjian',
        '9': 'iconfont icon-danju',
        '8': 'iconfont icon-baobiao',
        '24': 'iconfont icon-showpassword',
        '26': 'iconfont icon-lock_fill',
        '27': 'iconfont icon-morentouxiang'
      },
      // 是否折叠
      isCollapse: false,
      // 被激活的链接地址
      activePath: '',
      // 登录用户姓名
      name: '',
      // 修改对话框显示或隐藏
      editDialogVisible: false,
      // 添加任务对话框显示或隐藏
      editTeskDialogVisible: false,
      editForm: {
        mima: ''
      },
      // 修改表单的验证规则对象
      editFormRules: {
        mima: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getMenuList()
    this.name = window.sessionStorage.getItem('name')
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单
    async getMenuList() {
      const { data: res } = await this.$http.get('menus?rid=' + window.sessionStorage.getItem('rid'))
      if (res.meta.status !== 200) return this.$router.push('/login')
      // if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      // console.log('菜单列表')
      // console.log(res)
    },
    // 点击按钮，切换菜单的折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
    async editmima() {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        // 发起修改用户密码的数据请求
        const { data: res } = await this.$http.put('editpassword?username=' + window.sessionStorage.getItem('username') + '&password=' + this.editForm.mima)
        if (res.meta.status === 404) {
          return this.$message.error('修改密码与原来密码一样，修改失败')
        }
        console.log('密码修改成功')
        console.log(res)
        // 关闭对话框
        this.editDialogVisible = false
        // 提示修改成功
        this.$message.success('密码修改成功！')
      })
    },
    editDialogClosed() {
      this.$refs.editFormRef.clearValidate()
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #364e6e;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  // background-color: #333744;
  background-color: #364e6e;
  // background-color: #ffffff;
  // border-right: 1px solid #DCDFE6;
  .el-menu {
    border-right: none;
  }
}

.el-main {
  // background-color: #eaedf1;
  background-color: #F5F7FA;
}

.iconfont {
  margin-right: 10px;
}

.toggle-button {
  background-color: #4a5064;
  // background-color: #F3F7F8;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
.navbar {
  height: 50px;
  overflow: hidden;
  position: relative;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);

  .hamburger-container {
    line-height: 46px;
    height: 100%;
    float: left;
    cursor: pointer;
    transition: background .3s;
    -webkit-tap-highlight-color:transparent;

    &:hover {
      background: rgba(0, 0, 0, .025)
    }
  }

  .breadcrumb-container {
    float: left;
  }

  .errLog-container {
    display: inline-block;
    vertical-align: top;
  }

  .right-menu {
    float: right;
    height: 100%;
    line-height: 50px;

    &:focus {
      outline: none;
    }

    .right-menu-item {
      display: inline-block;
      padding: 0 8px;
      height: 100%;
      font-size: 18px;
      color: #5a5e66;
      vertical-align: text-bottom;

      &.hover-effect {
        cursor: pointer;
        transition: background .3s;

        &:hover {
          background: rgba(0, 0, 0, .025)
        }
      }
    }
  }
}
.avatar-container {
  margin-right: 30px;

  .avatar-wrapper {
    margin-top: 5px;
    position: relative;

    .user-avatar {
      cursor: pointer;
      width: 40px;
      height: 40px;
      border-radius: 20px;
    }

    .el-icon-caret-bottom {
      cursor: pointer;
      position: absolute;
      right: -20px;
      top: 25px;
      font-size: 12px;
    }
  }
}
</style>
